package com.hanock.sm.fork2d;

import java.util.Random;

import android.graphics.Rect;
import android.graphics.RectF;

public class MathUtil {
	
	private static Random r = new Random();

	public static int random(int min, int max){
		if (min > max){
			int t = max;
			max = min;
			min = t;
		}
		return r.nextInt(max - min + 1) + min;
	}
	
	public static boolean contains(RectF rect, float x, float y){
		return rect.left <= x && x <= rect.right && rect.bottom <= y && y <= rect.top;
	}
	
	public static void scaleRect(Rect rect, float sx, float sy){
		
		int cx = rect.centerX();
		int cy = rect.centerY();
		int w = (int)(rect.width()*sx);
		int h = (int)(height(rect)*sy);
		
		rect.left = cx - w/2;
		rect.right = cx + w/2;
		rect.top = cy + h/2;
		rect.bottom = cy - h/2;
	}
	
	public static void scaleRect(RectF rect, float sx, float sy){
		
		float cx = rect.centerX();
		float cy = rect.centerY();
		float w = rect.width()*sx;
		float h = height(rect)*sy;
		
		rect.left = cx - w/2;
		rect.right = cx + w/2;
		rect.top = cy + h/2;
		rect.bottom = cy - h/2;
	}
	
	public static void translateRect(Rect rect, float tx, float ty){
		
		rect.left += tx;
		rect.right += tx;
		rect.top += ty;
		rect.bottom += ty;
	}
	
	public static void translateRect(RectF rect, float tx, float ty){
		
		rect.left += tx;
		rect.right += tx;
		rect.top += ty;
		rect.bottom += ty;
	}
	
	public static float xFromRect(RectF rect, float ratio){
		
		return rect.left + rect.width()*ratio;
	}
	
	public static float yFromRect(RectF rect, float ratio){
		
		return rect.bottom + Math.abs(rect.height())*ratio;
	}	
	
	public static float height(RectF rect){
		return Math.abs(rect.top - rect.bottom);
	}
	
	public static int height(Rect rect){
		return Math.abs(rect.top - rect.bottom);
	}
}
